package com.qlhx.common;

import com.qlhx.wechat.common.util.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;

/**
 * Created by yxn on 2018-08-20.
 */
public class VideoCallBackCache {

    static JedisManager Vc ;


    public VideoCallBackCache(){

    }
    public VideoCallBackCache(JedisManager jedisManager){
        this.Vc  = jedisManager;
    }

    public static void init(JedisManager jedisManager){
        Vc = jedisManager;
    }


    /**
     * 判断是否存在
     * @param existskey
     * @return
     */
    public static boolean exists(String existskey){
        Jedis jds = null;
        boolean isBroken = false;
        try {
            jds = Vc.getJedis();
            jds.select(0);
            return  jds.exists(existskey);
        } catch (Exception e) {
            isBroken = true;
            e.printStackTrace();
        } finally {
            returnResource(jds, isBroken);
        }
        return false;
    }

    /**
     * 过期时间的
     *
     * @param key
     * @param value
     * @param timer （秒）
     */
    public static void setex(String key, String value, int timer) {
        Jedis jds = null;
        boolean isBroken = false;
        try {
            jds = Vc.getJedis();
            jds.select(0);
            jds.setex(key, timer, value);
        } catch (Exception e) {
            isBroken = true;
            e.printStackTrace();
        } finally {
            returnResource(jds, isBroken);
        }

    }


    /**
     * 简单的set
     * @param key
     * @param value
     */
    public static void set(String key ,String value){
        Jedis jds = null;
        boolean isBroken = false;
        try {
            jds = Vc.getJedis();
            jds.select(0);
            jds.set(key, value);
        } catch (Exception e) {
            isBroken = true;
            e.printStackTrace();
        } finally {
            returnResource(jds, isBroken);
        }
    }

    /**
     * 获取缓存数据
     * @param key
     * @return
     */
    public static String get(String key) {
        Jedis jds = null;
        boolean isBroken = false;
        try {
            jds = Vc.getJedis();
            jds.select(0);
            String value = jds.get(key);
            return value;
        } catch (Exception e) {
            isBroken = true;
            e.printStackTrace();
        } finally {
            returnResource(jds, isBroken);
        }
        return null;
    }

    /**
     * 删除Set
     * @param dkey
     * @return
     */
    public static Long delSetByKey(String key,String...dkey){
        Jedis jds = null;
        boolean isBroken = false;
        try {
            jds = Vc.getJedis();
            jds.select(0);
            Long result = 0L;
            if(null == dkey){
                result = jds.srem(key);
            }else{
                result = jds.del(key);
            }
            return result;
        } catch (Exception e) {
            isBroken = true;
            e.printStackTrace();
        } finally {
            returnResource(jds, isBroken);
        }
        return new Long(0);
    }

    /**
     * 释放
     *
     * @param jedis
     * @param isBroken
     */
    public static void returnResource(Jedis jedis, boolean isBroken) {
        if (jedis == null) {
            return;
        }
//        if (isBroken)
//            J.getJedisPool().returnBrokenResource(jedis);
//        else
//        	J.getJedisPool().returnResource(jedis);
//        版本问题
        jedis.close();
    }
}
